<template>
  <el-dialog class="edit-password" title="地址信息" append-to-body :close-on-click-modal="closeOnclickModal"
             width="500px" top="3.8vh" :visible.sync="isDialogShow" @close="close">
    <el-form :model="dialogForm" ref="dialogForm" :size="size" label-width="100px">
      <el-form-item :rules="yh_rules({required:true})" label="姓名" prop="contact">
        <el-input v-model="dialogForm.contact" placeholder="请输入姓名" clearable />
      </el-form-item>
      <el-form-item :rules="yh_rules({required:true, type: 'phone'})" label="手机号" prop="mobile">
        <el-input v-model="dialogForm.mobile" placeholder="请输入手机号(暂不支持座机)" clearable />
      </el-form-item>
      <el-form-item :rules="yh_rules({required:true})" label="所在地区" prop="city">
        <YhCityCascader @cityChange="cityChange" :renderAddr="addressStr"></YhCityCascader>
      </el-form-item>
      <el-form-item :rules="yh_rules({required:true})" label="详细地址" prop="address">
        <el-input type="textarea" :rows="2"
          placeholder="请输入详细地址"
          v-model="dialogForm.address">
        </el-input>
      </el-form-item>
      <el-form-item label="智能地址识别">
        <ZhiNengLocation ref="zhinengLocation" @parseAddress="parseAddress"></ZhiNengLocation>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="close" :size="size">取消</el-button>
      <el-button type="primary" @click="submit" :size="size">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {mapGetters} from 'vuex'
  import api from '@/utils/api.js'
  import YhCityCascader from '@/components/YhCityCascader'
  import ZhiNengLocation from '@/components/ZhiNengLocation'
  export default {
    name: 'EditAddressModal',
    components: {
      YhCityCascader,
      ZhiNengLocation
    },
    props: {
      addrDialogShow: {
        type: Boolean,
        default: false
      },
      propAddrInfo: {
        type: Object
      }
    },
    data () {
      return {
        isDialogShow: false,
        dialogForm: {
          contact: '',
          mobile: '',
          province: '',
          city: '',
          county: '',
          town: '',
          address: ''
        },
        zhiNengAddress: ''
      }
    },
    computed: {
      ...mapGetters(['size', 'closeOnclickModal', 'zhiXiaShi']),
      addressStr () {
        let {province, city, county, town} = this.dialogForm
        if (this.zhiXiaShi.includes(province)) {
          province = ''
        }
        return `${province}${city}${county || ''}${town || ''}`
      },
    },
    watch: {
      addrDialogShow (newValue) {
        this.isDialogShow = newValue
        if (newValue && this.propAddrInfo.id) {
          Object.assign(this.dialogForm, this.propAddrInfo)
        }
      }
    },
    methods: {
      cityChange (addrInfo) {
        Object.assign(this.dialogForm, addrInfo)
      },
      parseAddress (addrInfo) {
        addrInfo.mobile = addrInfo.mobile || this.dialogForm.mobile
        addrInfo.contact = addrInfo.contact || this.dialogForm.contact
        Object.assign(this.dialogForm, addrInfo)
      },
      // 提交
      submit () {
        this.$validateSubmit('dialogForm').then(() => {
          api.saveAddress(this.dialogForm).then(res => {
            this.$message.success('操作成功')
            this.$emit('refreshData')
            this.close()
          })
        })
      },
      close () {
        this.dialogForm = {
          contact: '',
          mobile: '',
          province: '',
          city: '',
          county: '',
          town: '',
          address: ''
        }
        this.$refs.zhinengLocation.location = ''
        this.$emit('update:addrDialogShow', false)
      },
    }
  }
</script>
